<?php

namespace app\controller;


use app\BaseController;

use app\model\Role;
use app\traits\CommonTrait;
use think\facade\Request;

class AdminRole extends BaseController
{
    use CommonTrait;

    public function store()
    {
        $slug = Request::post('slug');
        $name = Request::post('name');
        if(!$slug){
            return $this->apiJson(-1,'请输入角色标识');
        }
        if(mb_strlen($slug)>50){
            return $this->apiJson(-1,'角色标识不能超过50个字');
        }
        $roleExist = Role::where(['slug'=>$slug])->findOrEmpty();
        if(!$roleExist->isEmpty()){
            return $this->apiJson(-1,'角色标识已存在');
        }
        if(!$name){
            return $this->apiJson(-1,'请输入角色名称');
        }
        if(mb_strlen($name)>50){
            return $this->apiJson(-1,'角色名称不能超过50个字');
        }
        Role::create(['slug'=>$slug,'name'=>$name]);
        return $this->apiJson(1,'success');
    }

    public function list()
    {
        $list = Role::whereIn('id',[2,3,4])->field('id,name')->select();
        return $this->apiJson(1,'success',$list);
    }
}
